package com.nativetalk.common.util;

import com.nativetalk.bean.study.TdNtStudyTeachUpdate;
import com.nativetalk.bean.teacher.TdNtTeacherGetui;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.Constants;
import com.nativetalk.common.getui.AndroidStudentGetui;
import com.nativetalk.common.getui.AndroidTeacherGetui;
import com.nativetalk.common.getui.IosStudentGetui;
import com.nativetalk.common.getui.IosTeacherGetui;
import com.nativetalk.dao.TdNtStudyTeachDao;
import org.apache.log4j.Logger;

/**
 * Created by lizhun on 16/1/7.
 */
public class CallTeacher extends Thread{
    private static Logger logger = Logger.getLogger(CallTeacher.class);
    public void run(TdNtStudyTeachDao tdNtStudyTeachDao,int sst_id,int cou_id,
                    String memb_name,String memb_head_portrait,String cou_english_name,TdNtTeacherGetui tdNtTeacherGetui,String type,String devicetoken)
    {
        if (devicetoken != null && !(devicetoken.equals(Constants.default_equ_id))) {
            if (type.equals("1")) {
                AndroidStudentGetui.Countdown("外教君","",devicetoken,30);
            } else {
                IosStudentGetui.Countdown("外教君","",devicetoken,30);
            }
        }
        if (MemcachedUtils.get(tdNtTeacherGetui.getToken()) != null) {
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult)MemcachedUtils.get(tdNtTeacherGetui.getToken());
            if (tdNtTeacherResult.getEqu_client_id() != null && !(tdNtTeacherResult.getEqu_client_id().equals("1212313fasdf"))) {
                if (tdNtTeacherGetui.getEqu_type().equals("1")) {
                    AndroidTeacherGetui.apnpush("NativeTalk","One student sent you a teaching request, check now! ",
                            tdNtTeacherGetui.getEqu_client_id(),sst_id,memb_name,memb_head_portrait
                            ,tdNtTeacherGetui.getToken(),tdNtTeacherGetui.getTea_head_portrait(),tdNtTeacherGetui.getTea_name(),cou_english_name);
                } else {
                    logger.info(tdNtTeacherGetui.getEqu_client_id());
                    IosTeacherGetui.apnpush("NativeTalk","One student sent you a teaching request, check now! ",
                            tdNtTeacherGetui.getEqu_client_id(),sst_id,memb_name,memb_head_portrait
                            ,tdNtTeacherGetui.getToken(),tdNtTeacherGetui.getTea_head_portrait(),tdNtTeacherGetui.getTea_name(),cou_english_name);

                }

            }


        }
        logger.info("tea:" + Constants.teacher_appId);
        int i = 0;
        while(true){
            TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtStudyTeachDao.findTdNtStudyTeachUpdate(sst_id);
            try {
                i = i+1;
                sleep(1*100);
                logger.info(i);
                //这里可以写你自己要运行的逻辑代码
                if ( !(tdNtStudyTeachDetail.getStudy_state().equals("0")) || i >= 1200 ) {
                    break;
                }

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

}
